//
//  COBaseApi.swift
//  CocoProject
//
//  Created by Vill on 2023/11/6.
//

import Foundation
import Moya

enum COBaseApi {
    
    case feedbackVersion(param:[String:Any])
    case numBlackPhone(param:[String:Any])     //拉黑，记录手机信息
    case phoneserialnumberAdd(param:[String:Any])
    case phoneserialnumberQuery(param:[String:Any])
    case uploadData(data:Data)
    case deleteAccount
    case logoutAccount
    case phoneDetails(param:[String:Any])
    case addPhoneSerialSim(param:[String:Any])
    case guestRegister(param:[String:Any])
    case streamerInfo(param:[String:Any])      // 用户详情
    case consumeZegoToken(param:[String:Any])  // 获取既构通话登录token
    case getNewZegoToken(param:[String:Any])   // 扣费获取并新的token
    case marketUpdate                          // 引导下载
    case marketQa(param:[String:Any])          // 系统Q&A
    case streamerHot(param:[String:Any])       // 广场最热
    case streamerNew(param:[String:Any])       // 广场最新
    case streamerFollowList(param:[String:Any])    // 我的关注（my follow）
    case followCount                           // 我的关注数
    case streamerFollow(param:[String:Any])    // follow
    case streamerUnFollow(param:[String:Any])  // unfollow
    case rankSeven
    case getListUserInfo(param:[String:Any])   // 会员聊天信息
    case getListStreamerInfo(param:[String:Any])   // 聊天用户信息
    case queryUser                            // 个人中心
    case editUser(param:[String:Any])         // 用户修改个人中心
    case emailCode(param:[String:Any])        // 发送邮箱验证码
    case privacyPolicyV2
    case priceList(param:[String:Any])        // 用户通过平台号查询金币价格
    case useriOSPay(param:[String:Any])       // 支付验证
    case mybalance                            // 金币查询
    case feedbackNew(param:[String:Any])      // 用户反馈
    case rcImtoken                            // 融云token
    case translate(param:[String:Any])         // 翻译
    case giftSearch
    case consumeChat(param:[String:Any])      // 聊天扣币
    case consumeGift(param:[String:Any])      // 会员打赏礼物
    case favorableParse(param:[String:Any])        // 好评率
    case sreamerPhoto(param:[String:Any])        // 主播图片
    case consumeOnline(param:[String:Any])        // 会员状态
    case consumeDelay(param:[String:Any])        // 网络延迟
    case praseTag(param:[String:Any])        // 获取tag
    case prasePraise(param:[String:Any])        // 评论
    case electionTen                            //随机电话10金币
    case kvaKol(param:[String:Any])           // 评论
    case kvaChannel(param:[String:Any])        //渠道绑定
    case popupsPrice(param:[String:Any])        //活动金币弹窗
    case bindFree                               //是否弹窗获取20金币
    case userGuestEmail(param:[String:Any])        //绑定邮箱
    case bindEmailGetCoins(param:[String:Any])     //绑定邮箱获取金币
}

extension COBaseApi:TargetType {

    var baseURL: URL {
        return URL(string: KbaseUrl)!
    }
    
    var path: String {
        switch self {
        case .phoneserialnumberAdd:
            return "/api/v1/app/client/phoneserialnumber/add"
        case .numBlackPhone:
            return "/api/v1/app/client/phoneserialnumber/black/phone"
        case .phoneserialnumberQuery:
            return "/api/v1/app/client/phoneserialnumber/black/condition"
        case .uploadData:
            return "/api/v1/app/client/upload"
        case .deleteAccount:
            return "/api/v1/app/client/delete"
        case .logoutAccount:
            return "/api/v1/app/client/user/logout"
        case .feedbackVersion:
            return "/api/v1/app/client/feedback/version/phone"
        case .phoneDetails:
            return "/api/v1/app/client/phoneserialnumber/phone/details"
        case .addPhoneSerialSim:
            return "/api/v1/app/client/phoneserialnumber/addPhoneSerialSim"
        case .guestRegister:
            return "/api/v1/app/client/user/guest"
        case .streamerInfo:
            return "/api/v1/app/client/streamer/streamer"
        case .consumeZegoToken:
            return "/api/v1/app/client/consume/token/V4"
        case .getNewZegoToken:
            return "/api/v1/app/client/consume/call/v4"
        case .marketUpdate:
            return "/api/v1/app/client/marketing/up"
        case .marketQa:
            return "/api/v1/app/client/marketing/qa"
        case .streamerHot:
            return "/api/v1/app/client/streamer/hot"
        case .streamerNew:
            return "/api/v1/app/client/streamer/news"
        case .streamerFollowList:
            return "/api/v1/app/client/discover/listFollow"
        case .followCount:
            return "/api/v1/app/client/discover/followCount"
        case .streamerFollow:
            return "/api/v1/app/client/discover/follow"
        case .streamerUnFollow:
            return "/api/v1/app/client/discover/unfollow"
        case .rankSeven:
            return "/api/v1/app/client/streamer/ranking/seven"
        case .getListUserInfo:
            return "/api/v1/app/client/discover/getListUserInfo"
        case .getListStreamerInfo:
            return "/api/v1/app/client/streamer/getListStremaerInfo"
        case .queryUser:
            return "/api/v1/app/client/queryUser"
        case .editUser:
            return "/api/v1/app/client/editUser"
        case .emailCode:
            return "/api/v1/app/client/user/email/code"
        case .privacyPolicyV2:
            return "/api/v1/app/client/agreement/privacy-policy-v2"
        case .priceList:
            return "/api/v1/app/client/getPrice"
        case .useriOSPay:
            return "/api/v1/app/client/ios/pay"
        case .mybalance:
            return "/api/v1/app/client/balance"
        case .feedbackNew:
            return "/api/v1/app/client/feedback/new"
        case .rcImtoken:
            return "/api/v1/app/client/message/get/token"
        case .translate:
            return "/api/v1/app/client/translate"
        case .giftSearch:
            return "/api/v1/app/client/gift/search"
        case .consumeChat:
            return "/api/v1/app/client/consume/chat"
        case .consumeGift:
            return "/api/v1/app/client/consume/gift"
        case .favorableParse:
            return "/api/v1/app/client/parse/favorable"
        case .sreamerPhoto:
            return "/api/v1/app/client/streamer/photo"
        case .consumeOnline:
            return "/api/v1/app/client/consume/online"
        case .consumeDelay:
            return "/api/v1/app/client/consume/delay"
        case .praseTag:
            return "/api/v1/app/client/parse/tag"
        case .prasePraise:
            return "/api/v1/app/client/parse/praise"
        case .electionTen:
            return "/api/v1/app/client/electionTen"
        case .kvaKol:
            return "/api/v1/app/client/kol"
        case .kvaChannel:
            return "/api/v1/app/client/channel"
        case .popupsPrice:
            return "/api/v1/app/client/popupsPrice"
        case .bindFree:
            return "/api/v1/app/client/bind/free"
        case .userGuestEmail:
            return "/api/v1/app/client/user/guestemail"
        case .bindEmailGetCoins:
            return "/api/v1/app/client/user/bind/email"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .phoneserialnumberAdd,.numBlackPhone,.uploadData,.phoneDetails,.addPhoneSerialSim,.guestRegister,
                .getNewZegoToken,.editUser,.feedbackNew,.prasePraise,.useriOSPay,.userGuestEmail,.bindEmailGetCoins:
            return .post
        default:
            return .get
        }
    }
    
    var task: Moya.Task {
        switch self {
        case .phoneserialnumberAdd(param: let param),.numBlackPhone(param: let param),.phoneDetails(param: let param),.addPhoneSerialSim(param: let param),
                .guestRegister(param: let param),.getNewZegoToken(param: let param),.editUser(param: let param),.useriOSPay(param: let param),.feedbackNew(param: let param),.prasePraise(param: let param),.userGuestEmail(param: let param),.bindEmailGetCoins(param: let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
        case .uploadData(data: let data):
            let formData = MultipartFormData(provider: .data(data), name: "file_data",
                                             fileName: "file.png", mimeType: "image/png")
            return .uploadMultipart([formData])
        case .phoneserialnumberQuery(param: let param),.feedbackVersion(param: let param),.streamerInfo(param: let param),
                .consumeZegoToken(param: let param),.marketQa(param: let param),.streamerHot(param: let param),
                .streamerNew(param: let param),.streamerFollowList(param: let param),
                .streamerFollow(param: let param),.streamerUnFollow(param: let param),
                .getListUserInfo(param: let param),.getListStreamerInfo(param: let param),
                .emailCode(param: let param),.priceList(param: let param),.translate(param: let param),.consumeChat(param: let param),.consumeGift(param: let param),.favorableParse(param: let param),.sreamerPhoto(param: let param),.consumeOnline(param: let param),.consumeDelay(param: let param),.praseTag(param: let param),.kvaKol(param: let param),.kvaChannel(param: let param),.popupsPrice(param: let param):
            return .requestParameters(parameters: param, encoding: URLEncoding.default)
        default:
            return .requestPlain
        }
    }
        
    var headers: [String : String]? {
        var version = ""
        guard let kversion = kFeedCheckVer.keyGetNSDefaultsKey() else{
            var params: [String: String] = [:]
            params["phone-system"] = "1"
            params["app-package"] = "H1"
            params["app-version"] = KBundleVersion
            return params
        }
        version = kversion as! String
        let feed = kFeedbackVer.keyGetNSDefaultsKey() as? Bool
        guard let token = COUserInfoManager.shared.curUser?.token else {
            var params: [String: String] = [:]
            params["phone-system"] = "1"
            params["app-package"] = "H1"
            params["app-version"] =  version.count > 0 ? version : KBundleVersion
            params["open-data"] = feed == true ? "0":"1"
            return params
        }
        
        var params: [String: String] = [:]
        params["phone-system"] = "1"
        params["app-package"] = "H1"
        params["app-version"] = version.count > 0 ? version : KBundleVersion
        params["open-data"] = feed == true ? "0":"1"
        params["token"] = token
        return params
    }
}
